package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.asn1.axdr.AxdrEnum;
import com.dl698.asn1.axdr.AxdrOptional;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

/**
 * ConnectResponseInfo类
 * 实现连接响应信息的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class ConnectResponseInfo implements AxdrType {

    private byte[] dataCode = null;
    private AxdrEnum connectResult = null;
    private AxdrOptional<SecurityData> securityData = new AxdrOptional<SecurityData>(new SecurityData(), false);

    public ConnectResponseInfo() {
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setAll(AxdrEnum connectResult, SecurityData securityData) {
        this.connectResult = connectResult;
        this.securityData.setValue(securityData);
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;

        this.connectResult = new AxdrEnum();
        this.connectResult.setConst();
        codeLength += this.connectResult.decode(input);

        this.securityData = new AxdrOptional<SecurityData>(new SecurityData(), false);
        try {
            codeLength += this.securityData.decode(input);
        } catch (java.io.IOException e) {
            throw new RuntimeException("IO exception during decode", e);
        }

        return codeLength;
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        int codeLength;

        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                try {
                    output.write(this.dataCode[i]);
                } catch (java.io.IOException e) {
                    throw new RuntimeException(e);
                }
            }
        } else {
            if (this.connectResult != null && this.securityData != null) {
                codeLength = 0;
                try {
                    codeLength += this.securityData.encode(output);
                    codeLength += this.connectResult.encode(output);
                } catch (java.io.IOException e) {
                    throw new RuntimeException("IO exception during encode", e);
                }
            } else {
                throw new RuntimeException("securityData or connectResult is null");
            }
        }
        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    @Override
    public String toString() {
        if (this.securityData != null && this.connectResult != null) {
            return "sequence: {" + "connectResult: " + this.connectResult + ", securityData: " + this.securityData
                    + "}";
        } else {
            return "securityData || connectResult is null ";
        }
    }

    public AxdrOptional<SecurityData> getSecurityData() {
        return securityData;
    }
}